/*!
 * \file frtds_convert_to_intn.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>
#include "frcomm.h"
#include "frmem.h"

#include "frbyte_buffer.h"
#include "frhex_dump.h"
#include "frlist.h"
#include "frhash2.h"
#include "conntrack.h"
#include "frreport_comm.h"
#include "frct_comm.h"
#include "frtds_convert.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __case1(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    int i_whole_flg = FR_YES;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_unsigned_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x01\xff\xff\xff\xff", 5);

    i_ret = frtds_convert_to_intn(p_st_data, i_whole_flg, i_endian, i_unsigned_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "255");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;

}

static void __case2(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    int i_whole_flg = FR_YES;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_unsigned_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x04\xff\xff\xff\xff", 5);

    i_ret = frtds_convert_to_intn(p_st_data, i_whole_flg, i_endian, i_unsigned_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "4294967295");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;

}

static void __case3(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    int i_whole_flg = FR_YES;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_unsigned_flg = FR_YES;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x08\xff\xff\xff\xff", 5);

    i_ret = frtds_convert_to_intn(p_st_data, i_whole_flg, i_endian, i_unsigned_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_FAIL);

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;

}

static void __case4(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    int i_whole_flg = FR_YES;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_unsigned_flg = FR_NO;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x01\xff\xff\xff\xff", 5);

    i_ret = frtds_convert_to_intn(p_st_data, i_whole_flg, i_endian, i_unsigned_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "-1");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;

}

static void __case5(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    int i_whole_flg = FR_YES;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_unsigned_flg = FR_NO;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x04\xff\xff\xff\xff", 5);

    i_ret = frtds_convert_to_intn(p_st_data, i_whole_flg, i_endian, i_unsigned_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);
    CU_ASSERT_STRING_EQUAL(p_st_value->p_field_value, "-1");

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;

}

static void __case6(void)
{
    Frbyte_Packet *p_st_data = bytebuff_creat();
    int i_whole_flg = FR_NO;
    LITTLE_BIG_ENDIAN i_endian = BYTE_BIG_ENDIAN;
    int i_unsigned_flg = FR_NO;
    Frdata_field *p_st_value = NULL;
    int i_ret = 0;

    bytebuff_add(p_st_data, (char *)"\x08\xff\xff\xff\xff", 5);

    i_ret = frtds_convert_to_intn(p_st_data, i_whole_flg, i_endian, i_unsigned_flg, &p_st_value);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);

    bytebuff_free(&p_st_data);
    frtds_free_param(&p_st_value);

    return;

}

int add_suite_frtds_convert_to_intn(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frtds_convert frtds_convert_to_intn()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
    CU_add_test(pSuite, "case6", __case6);
//    CU_add_test(pSuite, "case7", __case7);

    return FR_SUCC;
}
